import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)
import login from '@/views/login/login.vue'
import layout from '@/views/layout/layout.vue'
import chart from '@/views/layout/chart/chart.vue'
import user from '@/views/layout/user/user.vue'
import question from '@/views/layout/question/question.vue'
import business from '@/views/layout/business/business.vue'
import subject from '@/views/layout/subject/subject.vue'

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: login,
      meta: { title: '登入页', roles: ['超级管理员', '管理员', '老师', '学生'] }
    },
    {
      path: '/layout',
      component: layout,
      redirect: '/layout/chart',
      children: [
        // {
        //   path: '/layout/',
        //   redirect: '/layout/chart'
        //   // component: chart
        // },
        {
          path: '/layout/chart',
          component: chart,
          meta: {
            title: '数据概览',
            roles: ['超级管理员', '管理员', '老师'],
            icon: 'el - icon - pie - chart'
          }
        },
        {
          path: '/layout/user',
          component: user,
          meta: {
            title: '用户列表',
            roles: ['超级管理员', '管理员'],
            icon: 'el - icon - user'
          }
        },
        {
          path: '/layout/question',
          component: question,
          meta: {
            title: '题库列表',
            roles: ['超级管理员', '管理员', '老师'],
            icon: 'el - icon - edit - outline'
          }
        },
        {
          path: '/layout/business',
          component: business,
          meta: {
            title: '企业列表',
            roles: ['超级管理员', '管理员', '老师'],
            icon: 'el - icon - office - building'
          }
        },
        {
          path: '/layout/subject',
          component: subject,
          meta: {
            title: '学科列表',
            roles: ['超级管理员', '管理员', '老师', '学生'],
            icon: 'el - icon - notebook - 2'
          }
        }
      ]
    }
  ]
})

import store from '@/store'
import { Message } from 'element-ui'
// import { removeToken } from '@/utils/local.js'
router.beforeEach((to, from, next) => {
  //路由进入前处理
  NProgress.start()
  if (to.meta.roles.includes(store.state.role)) {
    next()
  } else {
    Message.error('你无权访问该页面')
    // removeToken('token')
    next('/layout/subject')
  }
})
router.afterEach(to => {
  //路由进入后的处理
  // setTimeout(() => {
  //   // 结束进度
  //   NProgress.done()
  // }, 10000)
  NProgress.done()
  document.title = to.meta.title
})
export default router
